﻿Imports Data

Public Class PagoProveedor

    Private Sub PagoProveedor_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargarCBXEstado()
        Me.CargarCBXMes()
        Me.CargarCBXProveedor()
        Me.CargarLV()
    End Sub

    Private Function CargarCBXProveedor() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Proveedores _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxProveedor.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxProveedor.DisplayMember = "Nombre"
                Me.cbxProveedor.ValueMember = "ID"
                Me.cbxProveedor.DataSource = lista
                Me.cbxProveedor.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXMes() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Meses _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxMes.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxMes.DisplayMember = "Nombre"
                Me.cbxMes.ValueMember = "ID"
                Me.cbxMes.DataSource = lista
                Me.cbxMes.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXEstado() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Estado _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxEstado.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxEstado.DisplayMember = "Nombre"
                Me.cbxEstado.ValueMember = "ID"
                Me.cbxEstado.DataSource = lista
                Me.cbxEstado.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Try
            Dim n_pago As New IngresoPago_Proveedor(IngresoPago_Proveedor.Acciones.Ingresar, Nothing)
            n_pago.ShowDialog()
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub

    Private Function CargarLV() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.lvPagoProveedor.Items.Clear()

                Dim id_estado As Integer = Me.cbxEstado.SelectedValue
                Dim id_mes As Integer = Me.cbxMes.SelectedValue
                Dim id_proveedor As Integer = Me.cbxProveedor.SelectedValue

                Dim lista As List(Of Data.Pagos_Proveedores) = (From x In db.Pagos_Proveedores _
                                                              Where x.id_estado = id_estado _
                                                              And x.id_mes = id_mes And _
                                                              x.id_proveedor = id_proveedor _
                                                              Select x).ToList

                For Each item In lista
                    Me.lvPagoProveedor.Items.Add(New ListViewItem(New String() {item.Proveedores.nombre, _
                                                                                item.Modo_Pago.nombre, _
                                                                                item.Meses.nombre, _
                                                                                item.monto}) With {.Name = item.id})
                Next

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnRefrescar_Click(sender As System.Object, e As System.EventArgs) Handles btnRefrescar.Click
        Try
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub VerChequesToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles VerChequesToolStripMenuItem.Click
        Try
            Dim cheques As New ChequeForm(Me.lvPagoProveedor.SelectedItems(0).Name)
            cheques.ShowDialog()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EditarPagoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditarPagoToolStripMenuItem.Click
        Try
            Dim id_pago As Integer = Me.lvPagoProveedor.SelectedItems(0).Name
            Dim m_pago As New IngresoPago_Proveedor(IngresoPago_Proveedor.Acciones.Modificar, id_pago)
            m_pago.ShowDialog()
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EliminarPagoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarPagoToolStripMenuItem.Click
        Try
            Me.Eliminar(Me.lvPagoProveedor.SelectedItems(0).Name)
            Me.CargarLV()
        Catch ex As Exception

        End Try
    End Sub

    Private Function Eliminar(ByVal id_pago As Integer) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim e_pago As Data.Pagos_Proveedores = db.Pagos_Proveedores.Where(Function(p) p.id = id_pago).Select(Function(p) p).FirstOrDefault
                Dim e_cheques As List(Of Data.Cheques) = db.Cheques.Where(Function(p) p.id_factira = id_pago).Select(Function(p) p).ToList

                For Each item In e_cheques
                    db.Cheques.DeleteOnSubmit(item)
                Next

                db.Pagos_Proveedores.DeleteOnSubmit(e_pago)

                db.SubmitChanges()


            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class